package com.xiaoping.recyclerview3;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private Context mContext;

    private ArrayList<String> mTitle=new ArrayList<>();

    public MyAdapter(Context context, ArrayList<String> title) {
        this.mContext = context;
        this.mTitle = title;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{
        //定义view
        private TextView mTextView;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            //获取view
            mTextView = itemView.findViewById(R.id.item_tv);
        }
    }

    @NonNull
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_layout,parent,false));
    }

    //notifyItemRangeInserted会执行，一开始也会执行
    //notifyItemRemoved和notifyItemMoved不会执行
    @Override
    public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {
        //Log.d("myadapter", "onbind");
        //操作view
        holder.mTextView.setText(mTitle.get(position));
        holder.mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("myadapter", "pos ==> " + position);
                //remove后没被rmove的的pos不变
                //insert后insert部分是0~num-1，其余pos不变
                //move虽然位置移动了，但是pos没变
                //Toast.makeText(mContext, "" + mTitle.get(position), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mTitle == null ? 0 : mTitle.size();
    }

}
